﻿using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using SimpleORM.Model.DataBaseModel.SYSModelmain;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleORM.Infrastructure.SYSApiAuthorize
{
    public class RequestFilter: IAsyncActionFilter
    {
        private readonly UserMage userMage;
        private readonly CacheManager cacheManager;

        public RequestFilter(UserMage _userMage, CacheManager _cacheManager)
        {
            userMage = _userMage;
            cacheManager = _cacheManager;
        }
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //注册用户信息
            UserMage userMageS = new UserMage();
            string userMagestr= string.Empty;
            if(cacheManager.Exists("A0001"))
            userMagestr= cacheManager.Get("A0001").ToString();
            if(userMagestr.ToString()!="")
            {
                 userMageS = JsonConvert.DeserializeObject<UserMage>(userMagestr.ToString())!;
            }
            
            userMage.Name = userMageS.Name;
            userMage.UserName = userMageS.UserName;
            userMage.Description = userMageS.Description;
            userMage.UUID = userMageS.UUID;
            await next();
        }
    }
}
